package org.example.web3j.service;

import io.reactivex.Flowable;
import org.example.web3j.contracts.CPAMM;
import org.springframework.stereotype.Service;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;

@Service
public class CPAMMService {
    public static void main(String[] args) throws Exception {
        Web3j web3j = Web3j.build(new HttpService("http://127.0.0.1:8545"));


        String contractAddress = "0x1291Be112d480055DaFd8a610b7d1e203891C274";

        String ACCOUNT1="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
        String PRIVATE_KEY1="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";

        Credentials credentials = Credentials.create(PRIVATE_KEY1);
        CPAMM cpamm = CPAMM.load(
                contractAddress,
                web3j,
                credentials,
                new DefaultGasProvider()
        );

        // 调用设置手续费率
        BigInteger send = cpamm.swapFee().send();
        TransactionReceipt send1 = cpamm.setSwapFee(new BigInteger("300")).send();
        BigInteger send2 = cpamm.swapFee().send();

        // 监听手续费变更事件
        Flowable<CPAMM.FeeUpdateEventResponse> flowable =
                cpamm.feeUpdateEventFlowable(
                        DefaultBlockParameterName.LATEST,
                        DefaultBlockParameterName.LATEST
                );

        flowable.subscribe(event -> {
            System.out.println("🚀 监听到事件 FeeUpdate:");
            System.out.println("旧手续费: " + event.oldSwapFee);
            System.out.println("新手续费: " + event.newSwapFee);
        });

    }
}
